package com.lsus.golkhadi.server;

import javax.servlet.http.HttpSession;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.lsus.golkhadi.client.ILoginService;
import com.lsus.golkhadi.shared.Player;
import com.lsus.golkhadi.shared.Position;


public class LoginServiceImpl extends RemoteServiceServlet implements ILoginService{

	private static HttpSession httpSession;
	
	@Override
	public Player userIsValid(String username, String password) {
		Player player = new Player();
		if (username.equals("ligaj")){
			player.setPosition(Position.PLAYER1);
		}
		else if (username.equals("suresh")){
			player.setPosition(Position.PLAYER2);			
		}
		else if (username.equals("subhash")){
			player.setPosition(Position.PLAYER3);
		}
		else if (username.equals("uttam")){
			player.setPosition(Position.PLAYER4);
		}
				
		httpSession = this.getThreadLocalRequest().getSession();
		String r = httpSession.getId();
		player.setSessionId(r);
		httpSession.setAttribute(r, username);
		return player;
	}
	
	public static boolean verifyAuthorization(String sessionId){
		if (httpSession!=null){
			if (httpSession.getAttribute(sessionId)!=null){
				return true;
			}
			return false;
		}
		return false;
	}
	
	public static String getUserName(String sessionId){
		if (LoginServiceImpl.verifyAuthorization(sessionId)){
			return (String)LoginServiceImpl.httpSession.getAttribute(sessionId);
		}
		return null;
	}
	
	
	
	
	
}
